package com.servlet.manager;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.dao.impl.SubstanceDaoImpl;
import com.po.Substance;
import com.po.User;
import com.utils.DateUtil;
import com.utils.UUIDUtils;

public class AddSubstanceServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		try {
			List<FileItem> list = upload.parseRequest(request);
			Substance ss = new Substance();
			ss.setSdate(new DateUtil().getDate());
			ss.setSauthor(((User)request.getSession().getAttribute("Manager")).getUtname());
			ss.setSid(new UUIDUtils().getId());
			ss.setSup((byte)1);
			//获取表单信息，封装p对象
			for(FileItem f : list){
				if(f.isFormField()){
					if("stitle".equals(f.getFieldName())){
						ss.setStitle(f.getString("utf-8"));
					}
					if("stype".equals(f.getFieldName())){
						ss.setStype(f.getString("utf-8"));
					}
					if("scomment".equals(f.getFieldName())){
						ss.setScomment(Byte.parseByte(f.getString("utf-8")));
					}
					if("scid".equals(f.getFieldName())){
						ss.setScid(f.getString("utf-8"));
					}
					if("svote".equals(f.getFieldName())){
						ss.setSvote(Byte.parseByte(f.getString("utf-8")));
					}
					if("context".equals(f.getFieldName())){
						ss.setScontent(f.getString("utf-8"));
					}
				}else{
					// 上传文件到服务器
					String savepath = this.getServletContext().getRealPath("upload");
					System.out.println("1");
					String filename = f.getName();//f.getSize();
					System.out.println("2");
					filename = new Date().getTime()+filename;
					System.out.println("3");
					savepath = savepath+"\\"+filename;
					System.out.println(savepath);
					f.write(new File(savepath));
					//获取图片的网络地址，封装到product对象
					String imgurl = request.getContextPath()+"/upload/"+filename;
					System.out.println(imgurl);
					ss.setSimgurl(imgurl);
				}
			}
			if(new SubstanceDaoImpl().save(ss)){
				request.getRequestDispatcher("/WEB-INF/manage/success.jsp").forward(request, response);
			}else{
				request.getRequestDispatcher("/WEB-INF/manage/failure.jsp").forward(request, response);
			}
			
			
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
